﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Hartley.ApplicationLogic;
using Hartley.DTOs;
using Hartley.Util;

namespace WebApplication1
{
    public partial class viewAllResults : System.Web.UI.Page
    {
        protected int Rows
        {
            get
            {
                return ViewState["Rows"] != null ? (int)ViewState["Rows"] : 0;
            }
            set
            {
                ViewState["Rows"] = value;
            }
        }

        // Columns property to hold the Columns in the ViewState
        protected int Columns
        {
            get
            {
                return ViewState["Columns"] != null ? (int)ViewState["Columns"] : 0;
            }
            set
            {
                ViewState["Columns"] = value;
            }
        }

        protected int al_currentPageNumber = 1;
        protected int AL_YEAR_PER_ROW = 4;
        protected int OL_YEAR_PER_ROW = 4;
        private const int AL_PAGE_SIZE = 2;
        List<ALResultsDTO> allALevelData;
        protected int ol_currentPageNumber = 1;
        private const int OL_PAGE_SIZE = 2;
        List<OLResultsDTO> allOLevelData;
        List<Int32> allOLevelDataYrs;
        List<Int32> allALevelDataYrs;
        List<string> allOLevelDataYrsString;
        List<string> allALevelDataYrsString;
        string queryString;
        StudentAL studentAL;
        SportsUtil sportsUtil;
        string yrWiseQueryString;
        protected void Page_Load(object sender, EventArgs e)
        {
            //if (!IsPostBack)
            //{

                BindALData();
                BindOLData();
            //}
        }

        private void BindALData()
        {

            studentAL = new StudentAL();
            sportsUtil = new SportsUtil();
            allALevelData = new List<ALResultsDTO>();
            List<Int32> allALevelDataOfOnePage = new List<Int32>();

            allALevelData = studentAL.GetAllALevelResults().Where(a=>a.NoOfAs >= 1).Where(b=>(b.NoOfAs + b.NoOfBs + b.NoOfCs) >=3).OrderByDescending(a => a.Year).ToList();
            allALevelDataYrs = allALevelData.Select(a=>a.Year).Distinct().ToList();
            //foreach (int alevelYr in allALevelDataYrs)
            //{
               
            //        allALevelDataYrsString.Add(alevelYr.ToString());
             
            //}

            //if it contains 9999 then move it to last 
            if (allALevelDataYrs.Contains(9999))
            {
                allALevelDataYrs.Remove(9999);
                allALevelDataYrs.Insert(allALevelDataYrs.Count,9999);
            }
            // get the total rows 
            double aleveltotalRows = (int)allALevelDataYrs.Count;
           

            lblAllALevelPage.Text = "Displaying all of the College A/L Results - Page ";
            lblAllALevelOf.Text = "  of  ";


            lblAllALevelTotalPage.Text = CalculateTotalPages(aleveltotalRows, "ALevel").ToString();

            int al_curr_pg_no;
            if (Session["al_curr_pg_no"] != null)
            {
                al_curr_pg_no = Convert.ToInt32(Session["al_curr_pg_no"]);
            }
            else
            {
                al_curr_pg_no = al_currentPageNumber;
            }

            //int al_curr_pg_no = Convert.ToInt32(Session["al_curr_pg_no"]);
           // int ol_curr_pg_no = Convert.ToInt32(Session["ol_curr_pg_no"]);
            lblAllALevelCurrPage.Text = al_curr_pg_no.ToString();
         

            if (Convert.ToInt32(lblAllALevelTotalPage.Text) < 3)
            {
                if (Int32.Parse(lblAllALevelTotalPage.Text) <= 0)
                {
                    btnAllALevelFirst.Enabled = false;
                    btnAllALevelPrev.Enabled = false;
                    btnAllALevelNext.Enabled = false;
                    btnAllALevelLast.Enabled = false;


                }

                if (al_curr_pg_no == 1)
                {
                    btnAllALevelFirst.Enabled = false;
                    btnAllALevelPrev.Enabled = false;



                    if (Int32.Parse(lblAllALevelTotalPage.Text) == 1)
                    {
                        allALevelDataOfOnePage = allALevelDataYrs.GetRange(0, Convert.ToInt32(aleveltotalRows));

                        btnAllALevelNext.Enabled = false;
                        btnAllALevelLast.Enabled = false;

                    }
                    else if (Int32.Parse(lblAllALevelTotalPage.Text) == 2)
                    {
                        allALevelDataOfOnePage = allALevelDataYrs.GetRange(0, AL_PAGE_SIZE * AL_YEAR_PER_ROW);

                        btnAllALevelNext.Enabled = true;
                        btnAllALevelLast.Enabled = true;

                    }
                }

                else
                {

                    btnAllALevelPrev.Enabled = true;
                    btnAllALevelFirst.Enabled = true;

                    if (al_curr_pg_no == Int32.Parse(lblAllALevelTotalPage.Text))
                    {
                        allALevelDataOfOnePage = allALevelDataYrs.GetRange((al_curr_pg_no - 1) * AL_PAGE_SIZE * AL_YEAR_PER_ROW
                            , (Convert.ToInt32(aleveltotalRows) - (al_curr_pg_no - 1) * AL_PAGE_SIZE * AL_YEAR_PER_ROW));

                        btnAllALevelNext.Enabled = false;
                        btnAllALevelLast.Enabled = false;

                    }
                    else
                    {
                        allALevelDataOfOnePage = allALevelDataYrs.GetRange((al_curr_pg_no - 1) * AL_PAGE_SIZE * AL_YEAR_PER_ROW, AL_PAGE_SIZE * AL_YEAR_PER_ROW);

                        btnAllALevelNext.Enabled = true;
                        btnAllALevelLast.Enabled = true;


                    }

                }
            }


            else
            {
                if (al_curr_pg_no == 1)
                {
                    allALevelDataOfOnePage = allALevelDataYrs.GetRange(0, AL_PAGE_SIZE * AL_YEAR_PER_ROW);

                    btnAllALevelFirst.Enabled = false;
                    btnAllALevelPrev.Enabled = false;
                    btnAllALevelNext.Enabled = true;
                    btnAllALevelLast.Enabled = true;

                }



                else
                {
                    btnAllALevelPrev.Enabled = true;
                    btnAllALevelFirst.Enabled = true;

                    if (al_curr_pg_no == Int32.Parse(lblAllALevelTotalPage.Text))
                    {
                        allALevelDataOfOnePage = allALevelDataYrs.GetRange((al_curr_pg_no - 1) * AL_PAGE_SIZE * AL_YEAR_PER_ROW
                            , (Convert.ToInt32(aleveltotalRows) - (al_curr_pg_no - 1) * AL_PAGE_SIZE * AL_YEAR_PER_ROW));

                        btnAllALevelNext.Enabled = false;
                        btnAllALevelLast.Enabled = false;

                    }
                    else
                    {
                        allALevelDataOfOnePage = allALevelDataYrs.GetRange((al_curr_pg_no - 1) * AL_PAGE_SIZE * AL_YEAR_PER_ROW, AL_PAGE_SIZE * AL_YEAR_PER_ROW);

                        btnAllALevelNext.Enabled = true;
                        btnAllALevelLast.Enabled = true;


                    }





                }
            }

                CreateDynamicTableforAL(allALevelDataOfOnePage);



                          
        }


        private void BindOLData()
        {

            studentAL = new StudentAL();
            sportsUtil = new SportsUtil();
            allOLevelData = new List<OLResultsDTO>();
            List<Int32> allOLevelDataOfOnePage = new List<Int32>();

            //queryString = Request.QueryString.ToString();
            //string afterRemovedPlusSign = "";
            //string afterReplaceComma = "";
            //string correctedQueryString = "";
            //string selectedCategory = "";



            allOLevelData = studentAL.GetAllOLevelResults().Where(a => a.NoOfAs >= 4).Where(b => (b.NoOfAs + b.NoOfBs + b.NoOfCs) >= 6).OrderByDescending(b => b.Year).ToList();
            allOLevelDataYrs = allOLevelData.Select(a => a.Year).Distinct().ToList();

            //if it contains 9999 then move it to last 
            if (allOLevelDataYrs.Contains(9999))
            {
                allOLevelDataYrs.Remove(9999);
                allOLevelDataYrs.Insert(allOLevelDataYrs.Count, 9999);
            }
            // get the total rows 
            double oleveltotalRows = (int)allOLevelDataYrs.Count;

            if (allOLevelDataYrs.Contains(9999))
            {
                allALevelDataYrs.Remove(9999);
                allALevelDataYrs.Insert(allALevelDataYrs.Count,9999);
            }
            
            lblAllOLevelPage.Text = "Displaying all of the College O/L Results - Page ";
            lblAllOLevelOf.Text = "  of  ";

            lblAllOLevelTotalPage.Text = CalculateTotalPages(oleveltotalRows, "OLevel").ToString();

            int ol_curr_pg_no;
            if (Session["ol_curr_pg_no"]!=null)
            {
                ol_curr_pg_no = Convert.ToInt32(Session["ol_curr_pg_no"]);
            }
            else
            {
                ol_curr_pg_no = ol_currentPageNumber;
            }
            //int al_curr_pg_no = Convert.ToInt32(Session["al_curr_pg_no"]);
            // int ol_curr_pg_no = Convert.ToInt32(Session["ol_curr_pg_no"]);
            lblAllOLevelCurrPage.Text = ol_curr_pg_no.ToString();

           
            if (Convert.ToInt32(lblAllOLevelTotalPage.Text) < 3)
            {
                if (Int32.Parse(lblAllOLevelTotalPage.Text) <= 0)
                {
                    btnAllOLevelFirst.Enabled = false;
                    btnAllOLevelPrev.Enabled = false;
                    btnAllOLevelNext.Enabled = false;
                    btnAllOLevelLast.Enabled = false;


                }

                if (ol_curr_pg_no == 1)
                {
                    btnAllOLevelFirst.Enabled = false;
                    btnAllOLevelPrev.Enabled = false;

                    if (Int32.Parse(lblAllOLevelTotalPage.Text) == 1)
                    {
                        allOLevelDataOfOnePage = allOLevelDataYrs.GetRange(0, Convert.ToInt32(oleveltotalRows));

                        btnAllOLevelNext.Enabled = false;
                        btnAllOLevelLast.Enabled = false;

                    }
                    else if (Int32.Parse(lblAllOLevelTotalPage.Text) == 2)
                    {
                        allOLevelDataOfOnePage = allOLevelDataYrs.GetRange(0, OL_PAGE_SIZE * OL_YEAR_PER_ROW);

                        btnAllOLevelNext.Enabled = true;
                        btnAllOLevelLast.Enabled = true;

                    }


                }

                else
                {


                    if (ol_curr_pg_no == Int32.Parse(lblAllOLevelTotalPage.Text))
                    {
                        allOLevelDataOfOnePage = allOLevelDataYrs.GetRange((ol_curr_pg_no - 1) * OL_PAGE_SIZE * OL_YEAR_PER_ROW
                            , (Convert.ToInt32(oleveltotalRows) - (ol_curr_pg_no - 1) * OL_PAGE_SIZE * OL_YEAR_PER_ROW));

                        btnAllOLevelNext.Enabled = false;
                        btnAllOLevelLast.Enabled = false;

                    }
                    else
                    {
                        allOLevelDataOfOnePage = allOLevelDataYrs.GetRange((ol_curr_pg_no - 1) * OL_PAGE_SIZE * OL_YEAR_PER_ROW, OL_PAGE_SIZE * OL_YEAR_PER_ROW);

                        btnAllOLevelNext.Enabled = true;
                        btnAllOLevelLast.Enabled = true;


                    }
                }



            }
            else
            {
                if (ol_curr_pg_no == 1)
                {
                    allOLevelDataOfOnePage = allOLevelDataYrs.GetRange(0, OL_PAGE_SIZE * OL_YEAR_PER_ROW);

                    btnAllOLevelFirst.Enabled = false;
                    btnAllOLevelPrev.Enabled = false;
                    btnAllOLevelNext.Enabled = true;
                    btnAllOLevelLast.Enabled = true;

                }



                else
                {
                    btnAllOLevelPrev.Enabled = true;
                    btnAllOLevelFirst.Enabled = true;

                    if (ol_curr_pg_no == Int32.Parse(lblAllOLevelTotalPage.Text))
                    {
                        allOLevelDataOfOnePage = allOLevelDataYrs.GetRange((ol_curr_pg_no - 1) * OL_PAGE_SIZE * OL_YEAR_PER_ROW
                            , (Convert.ToInt32(oleveltotalRows) - (ol_curr_pg_no - 1) * OL_PAGE_SIZE * OL_YEAR_PER_ROW));

                        btnAllOLevelNext.Enabled = false;
                        btnAllOLevelLast.Enabled = false;

                    }
                    else
                    {
                        allOLevelDataOfOnePage = allOLevelDataYrs.GetRange((ol_curr_pg_no - 1) * OL_PAGE_SIZE * OL_YEAR_PER_ROW, OL_PAGE_SIZE * OL_YEAR_PER_ROW);

                        btnAllOLevelNext.Enabled = true;
                        btnAllOLevelLast.Enabled = true;


                    }


                }
            }




            // gvAllMeeting.DataSource = allMeetingsOfOnePage;
            // gvAllMeeting.DataBind();


            CreateDynamicTableforOL(allOLevelDataOfOnePage);



        }

        private int CalculateTotalPages(double totalRows , string type)
        {
             int totalPages=0;
            if(type.Equals("ALevel"))
            {
                totalPages = (int)Math.Ceiling(totalRows / (AL_PAGE_SIZE * AL_YEAR_PER_ROW));
            }
            else if (type.Equals("OLevel"))
            {
                totalPages = (int)Math.Ceiling(totalRows / (OL_PAGE_SIZE * OL_YEAR_PER_ROW));
            }
            
            return totalPages;
        }

        protected void ChangePageOL(object sender, CommandEventArgs e)
        {

            switch (e.CommandName)
            {

                case "First":
                    ol_currentPageNumber = 1;
                    break;

                case "Previous":
                    ol_currentPageNumber = Int32.Parse(lblAllOLevelCurrPage.Text) - 1;
                    break;

                case "Next":
                    ol_currentPageNumber = Int32.Parse(lblAllOLevelCurrPage.Text) + 1;
                    break;
                case "Last":
                    ol_currentPageNumber = Int32.Parse(lblAllOLevelTotalPage.Text);
                    break;
            }

            //Session["ol_curr_pg_no"] = ol_currentPageNumber;

            //int ol_curr_pg_no;
            //if (Session["ol_curr_pg_no"] != null)
           // {
                Session["ol_curr_pg_no"] = ol_currentPageNumber;
           
            BindOLData();
        }

        protected void ChangePageAL(object sender, CommandEventArgs e)
        {

            switch (e.CommandName)
            {

                case "First":
                    al_currentPageNumber = 1;
                    break;

                case "Previous":
                    al_currentPageNumber = Int32.Parse(lblAllALevelCurrPage.Text) - 1;
                    break;

                case "Next":
                    al_currentPageNumber = Int32.Parse(lblAllALevelCurrPage.Text) + 1;
                    break;
                case "Last":
                    al_currentPageNumber = Int32.Parse(lblAllALevelTotalPage.Text);
                    break;
            }

            Session["al_curr_pg_no"] = al_currentPageNumber;
            BindALData();
        }

        private void CreateDynamicTableforAL(List<Int32> allALevelDataOfOnePage)
        {
           PlaceHolder1.Controls.Clear();

            // Fetch the number of Rows and Columns for the table 
            // using the properties


            int tblRows = allALevelDataOfOnePage.Count / AL_YEAR_PER_ROW;
            int tblCols = 0;


            // Create a Table and set its properties 
            Table tbl = new Table();
            tbl.Width = new Unit("100%");
            tbl.BorderStyle = BorderStyle.Solid;
            // Add the table to the placeholder control
            PlaceHolder1.Controls.Add(tbl);
            // Now iterate through the table and add your controls 
            for (int i = 0; i <= tblRows; i++)
            {
                TableRow tr = new TableRow();
                tr.Width = new Unit("25%");



                if (i != tblRows && tblRows >= 0)
                {
                    tblCols = AL_YEAR_PER_ROW;
                }
                else
                {
                    tblCols = allALevelDataOfOnePage.Count % AL_YEAR_PER_ROW;
                }


                for (int j = 0; j < tblCols; j++)
                {
                    TableCell tc = new TableCell();

                    HyperLink hlAthleticYrCell = new HyperLink();

                    //change the text to N/A if the yr is 9999

                    if (allALevelDataOfOnePage[i * AL_YEAR_PER_ROW + j] != 9999)
                    {
                        hlAthleticYrCell.Text = allALevelDataOfOnePage[i * AL_YEAR_PER_ROW + j].ToString();
                        
                    }
                    else
                    {
                        hlAthleticYrCell.Text = "N/A";
                    }

                    if (string.IsNullOrEmpty(yrWiseQueryString))
                    {
                        hlAthleticYrCell.NavigateUrl = "viewBestResults.aspx?" +"SelectedExamType=ALevel"+"&Year="+  sportsUtil.encryptQueryStrings( allALevelDataOfOnePage[i * AL_YEAR_PER_ROW + j].ToString());
                    }
                    if (i % 2 == 0)
                    {
                        tc.Style.Add("background-color", "#003399");
                        tc.ForeColor = System.Drawing.Color.White;
                    }
                    else
                    {
                        tc.Style.Add("background-color", "White");
                        tc.ForeColor = System.Drawing.Color.Blue;
                    }



                    // Add the control to the TableCell
                    tc.Controls.Add(hlAthleticYrCell);
                    // Add the TableCell to the TableRow
                    tr.Cells.Add(tc);
                }

                // Add the TableRow to the Table
                tbl.Rows.Add(tr);
            }

            // This parameter helps determine in the LoadViewState event,
            // whether to recreate the dynamic controls or not

            ViewState["dynamictable"] = true;
        }

        private void CreateDynamicTableforOL(List<Int32> allOLevelDataOfOnePage)
        {
           PlaceHolder2.Controls.Clear();

            // Fetch the number of Rows and Columns for the table 
            // using the properties


            int tblRows = allOLevelDataOfOnePage.Count / OL_YEAR_PER_ROW;
            int tblCols = 0;


            // Create a Table and set its properties 
            Table tbl = new Table();
            tbl.Width = new Unit("100%");
            tbl.BorderStyle = BorderStyle.Solid;
            // Add the table to the placeholder control
            PlaceHolder2.Controls.Add(tbl);
            // Now iterate through the table and add your controls 
            for (int i = 0; i <= tblRows; i++)
            {
                TableRow tr = new TableRow();
                tr.Width = new Unit("25%");



                if (i != tblRows && tblRows >=0 )
                {
                    tblCols = OL_YEAR_PER_ROW;
                }
                else
                {
                    tblCols = allOLevelDataOfOnePage.Count % OL_YEAR_PER_ROW;
                }


                for (int j = 0; j < tblCols; j++)
                {
                    TableCell tc = new TableCell();

                    HyperLink hlAthleticYrCell = new HyperLink();

                    if (allOLevelDataOfOnePage[i * OL_YEAR_PER_ROW + j] != 9999)
                    {
                        hlAthleticYrCell.Text = allOLevelDataOfOnePage[i * OL_YEAR_PER_ROW + j].ToString();
                    }
                    else
                    {
                        hlAthleticYrCell.Text = "N/A";
                    }

                    if (string.IsNullOrEmpty(yrWiseQueryString))
                    {
                        hlAthleticYrCell.NavigateUrl = "viewBestResults.aspx?" + "SelectedExamType=OLevel" + "&Year="+allOLevelDataOfOnePage[i * OL_YEAR_PER_ROW + j];
                    }
                    if (i % 2 == 0)
                    {
                        tc.Style.Add("background-color", "#003399");
                        tc.ForeColor = System.Drawing.Color.White;
                    }
                    else
                    {
                        tc.Style.Add("background-color", "White");
                        tc.ForeColor = System.Drawing.Color.Blue;
                    }



                    // Add the control to the TableCell
                    tc.Controls.Add(hlAthleticYrCell);
                    // Add the TableCell to the TableRow
                    tr.Cells.Add(tc);
                }

                // Add the TableRow to the Table
                tbl.Rows.Add(tr);
            }

            // This parameter helps determine in the LoadViewState event,
            // whether to recreate the dynamic controls or not

            ViewState["dynamictable"] = true;
        }

        protected void btnAllALevelPrev_Click(object sender, EventArgs e)
        {

        }
    }
}